package net.sevear.chaos.view
{
	import com.leebrimelow.drawing.Wedge;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.Image;
	import mx.core.INavigatorContent;
	import mx.core.UIComponent;
	import mx.effects.Rotate;
	import mx.effects.easing.Linear;
	import mx.events.EffectEvent;
	import mx.events.ToolTipEvent;
	
	import net.sevear.chaos.event.FanRollEvent;
	
	public class FanRoller extends UIComponent
	{
		private static const CLOCKWISE:int = 0;
		private static const ANTICLOCKWISE:int = 1;
		
		[Embed(source='/res/fan.png')]
		private var fancls:Class;
		
		private var rollImg:Image;
		private var bmp:Bitmap;
		private var startA:Number;
		private var endA:Number;
		private var imguc:UIComponent;
		private var lastA:Number;
		public function FanRoller(w:Number,h:Number)
		{
			super();
			this.width = w;
			this.height = h;
			
			var cls:Class = fancls;
			bmp = new cls() as Bitmap;
			(bmp).smoothing = true;
			bmp.width = w;
			bmp.height = h;
			imguc = new UIComponent();
			bmp.x = -w/2;
			bmp.y = -h/2;
			imguc.addChild(bmp);
			imguc.x = w/2;
			imguc.y = h/2;
			imguc.alpha = 0.44;
			this.buttonMode = true;
			this.addChild(imguc);
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			this.addEventListener(MouseEvent.RIGHT_CLICK,cancelHandRoll);
//			this.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		
		private var _opEnable:Boolean = true;
		public function set enable(value:Boolean):void{
			_opEnable = value;
			if(_opEnable){
				if(this.hasEventListener(MouseEvent.MOUSE_DOWN)==false){
					this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
				}
				if(this.hasEventListener(MouseEvent.MOUSE_UP)==false){
					this.addEventListener(MouseEvent.MOUSE_UP,onMouseRelease);
				}
			}else{
				if(this.hasEventListener(MouseEvent.MOUSE_DOWN)==true){
					this.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
				}
				if(this.hasEventListener(MouseEvent.MOUSE_UP)==true){
					this.removeEventListener(MouseEvent.MOUSE_UP,onMouseRelease);
				}
			}
		}
		
		private var motion:Rotate;
		private var isRolling:Boolean = false;
		private var _passive:Boolean;
		public function startRolling(count:int,passive:Boolean = false):void{
			if(motion==null || motion.isPlaying==false){
				_passive = passive;
				if(motion==null){
					motion = new Rotate(imguc);
					motion.angleFrom = imguc.rotation;
					motion.easingFunction = Linear.easeOut;
					motion.addEventListener(EffectEvent.EFFECT_END,onRollingEnd);
				}
				isRolling = true;
				motion.duration = 500*count;
				motion.angleTo = 360*count+imguc.rotation;
				motion.play();
			}
		}
		
		
		private function onRollingEnd(e:EffectEvent):void{
			isRolling = true;
			if(_passive==false)
				dispatchEvent(new FanRollEvent(FanRollEvent.ROLLING_EFFECT_END));
		}
		
		private var _intervalAngle:Number;
		private var _intervalArr:Array;
		private var _bgSprits:Array;
		
		public function setInterval(total:int):void{
			if(total>0){
				_intervalAngle = 360/total;
				_intervalArr =[];
				if(_bgSprits==null) _bgSprits = [];
				var bgSprite:UIComponent;
				var i:int=0;
				for (;i<total;i++){
					_intervalArr.push((i+0.5)*_intervalAngle);
					
					if(_bgSprits.length>i){
						bgSprite =_bgSprits[i] as UIComponent;
					}else{
						bgSprite = new UIComponent();
						bgSprite.width = bmp.width;
						bgSprite.height = bmp.height;
						bgSprite.x = 4;
						bgSprite.y = bmp.height+4;
						bgSprite.toolTip = i.toString();
						bgSprite.rotation =270;
						this.addChildAt(bgSprite,0);
						_bgSprits.push(bgSprite);
					}
					bgSprite.alpha = 0.66;
					bgSprite.graphics.clear();
//					var color:uint = colors[i];
//					bgSprite.graphics.beginFill(color,1);
//					trace(i,color.toString(16),_intervalAngle,i*_intervalAngle);
//					Wedge.draw(bgSprite, bmp.width/2, bmp.height/2, Math.random()*bmp.width/2+10, (i+1)*_intervalAngle, 0);
				}
				
				while(i<_bgSprits.length){
					bgSprite =_bgSprits[i] as UIComponent;
					bgSprite.graphics.clear();
					i++;
				}
			}else{
				trace("分割数不能<=0")
				this.enabled = false;
			}
		}
		
		public function setSelectionStyle(idx:int,selected:Boolean):void{
			var bgUC:UIComponent = _bgSprits[idx] as UIComponent;
			if(bgUC!=null){
				bgUC.alpha = selected?1:0.66;
			}
		}
		
		private function generateDrawCmd(startA:Number,endA:Number,radius:Number):void{
			//FIXME:
			var cmdTotal:int = Math.floor(endA-startA);
			
		}
		
		private function onClick(e:MouseEvent):void{
			trace("fan click");
		}
		
		private function onToolTipStart(e:ToolTipEvent):void{
			trace(mouseX,mouseY)
		}
		
		private function onMouseDown(e:MouseEvent):void{
			if(motion==null || motion.isPlaying==false){
				lastA = startA = caculateMouseAngle(mouseX,mouseY);
				imguc.rotation = startA;
//				trace("start angle:",startA);
				if(imguc.hasEventListener(MouseEvent.MOUSE_MOVE)==false){
					imguc.addEventListener(MouseEvent.MOUSE_MOVE,onMouseOver);
				}
				if(this.hasEventListener(MouseEvent.MOUSE_UP)==false){
					this.addEventListener(MouseEvent.MOUSE_UP,onMouseRelease);
				}
			}
		}
		
		private function cancelHandRoll(e:MouseEvent):void{
			if(imguc.hasEventListener(MouseEvent.MOUSE_MOVE)){
				imguc.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseOver);
			}
			if(this.hasEventListener(MouseEvent.MOUSE_UP)==true){
				this.removeEventListener(MouseEvent.MOUSE_UP,onMouseRelease);
			}
			endA = caculateMouseAngle(mouseX,mouseY);
//			trace("end angle:",endA);
		}
		
		private function caculateMouseAngle(xpos:Number,ypos:Number):Number{
			var centerP:Point = new Point(bmp.width/2,bmp.height/2);
//			if(ypos-centerP.y==0 && xpos-centerP.x==0){
//				trace(Math.atan2(ypos-centerP.y,xpos-centerP.x)*180/Math.PI);
//			}
			var originA:Number = Math.atan2(ypos-centerP.y,xpos-centerP.x)*180/Math.PI;
			if(originA>=0){
				return originA+90;
			}else{
				if(originA>=-90){
					return originA+90;
				}else{
					return 450+(originA);
				}
			}
		}
		
		private function onMouseRelease(e:MouseEvent = null):void{
			if(imguc.hasEventListener(MouseEvent.MOUSE_MOVE)){
				imguc.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseOver);
			}
			endA = caculateMouseAngle(mouseX,mouseY);
//			trace("end angle:",endA);
			var ev:FanRollEvent = new FanRollEvent(FanRollEvent.DRAG_OVER);
			dispatchEvent(ev);
		}
		
		private function onMouseOver(e:MouseEvent):void{
			var newR:Number = caculateMouseAngle(mouseX,mouseY);
			var oldR:Number = 0;
			if(imguc.rotation >=0){
				oldR =  imguc.rotation;
			}else{
				oldR = 360+imguc.rotation;
			}
			for each(var p:Number in _intervalArr){
				if((oldR<p && newR>=p)||(newR<p && oldR>=p)){
					if(Math.abs(newR-oldR)<=_intervalAngle){
//						trace("pass a interval point.",p,imguc.rotation,oldR,newR);
						var idx:int = _intervalArr.indexOf(p);
						var ev:FanRollEvent = new FanRollEvent(FanRollEvent.PASSING,idx);
						dispatchEvent(ev);
						break;
					}
				}
			}
			imguc.rotation = newR;
			
		}
		
		private function caculateFanAngle(xpos:Number,ypos:Number):Number{
			var centerP:Point = new Point(rollImg.width/2,rollImg.height/2);
			var originA:Number = Math.atan2(ypos-centerP.y,xpos-centerP.x)*180/Math.PI;
			return originA;
		}
		
	}
}